var _inherit = require('core/inherit.js');
var R = require('res/texture.js').R;
var S = require('res/style.js');
var _Constant = require('scenes/game/constant.js');
var _Container = require('component/container.js');
var _layout = require('tools/layout.js');
var _relative = _layout.relative;
var _Button = require('widget/button.js');

var _global = require('global/global.js');
var R = require('res/texture.js').R;

var UPDOWN_OFFSET = 8;

// ==========================
// TextRef
// ==========================
function TextRef(text) {
    _Container.call(this);
    this.mText = text;
}
_inherit(TextRef, _Container);
TextRef.prototype.drawContent = function (context) {
    this.mText.draw(context);
};

exports.dialogBg = function (height) {
    var pic = _global.ninePatchNode(R.dialog.bg, 'v', 45, 60);
    pic.setSize(0, height);
    return pic;
};
exports.inputBg = function (width) {
    var pic = _global.ninePatchNode(R.game.grid_bg, 'h', 12, 12);
    pic.setSize(width, 0);
    return pic;
};
exports.createRecordBg = function () {
    var width = _Constant.recordSlot[0] * 4;
    var height = _Constant.recordSlot[1] * 4;

    var mh = 10;
    var mv = 10;
    var bg = _global.ninePatchNode(R.dialog.text_bg, 'all', 10, 20, 10, 10);
    bg.setSize(width + 2 * mh, height + 2 * mh);
    bg.setPosition(-mh, -mv)

    return bg;
};
exports.createDialogBg = function (row, col) {
    var width = _Constant.slotSize[0] * col;
    var height = _Constant.slotSize[1] * row;

    var mx = 16;
    var my = 20;

    var pic = _global.ninePatchNode(R.dialog.text_bg, 'all', 12, 20, 12, 10);
    pic.setSize(width + mx, height + my);
    pic.setPosition(-mx * 0.5, -my * 0.5);
    return pic;
};
exports.createGridBg = function (row, col) {
    var width = _Constant.slotSize[0] * col;
    var height = _Constant.slotSize[1] * row;

    var mx = 14;
    var my = 16;

    var pic = _global.ninePatchNode(R.game.grid_bg, 'all', 12, 20, 12, 10);
    pic.setSize(width + mx, height + my);
    pic.setPosition(-mx * 0.5, -my * 0.5);
    return pic;
};
exports.createLongButton = function (up, down, id, text) {
    var width = 306;
    var pic = _global.ninePatchNode(up, 'h', 22, 22);
    pic.setSize(width, 0);

    var text = S.ui.longBt.createText(text);
    text.setAnchor(0.5, 0.5);

    var cup = new _Container();
    cup.addChild(pic);
    cup.setSize(pic.getSize());
    cup.addChild(text);
    _relative.local.layoutTo(text, 0.5, 0.5, cup, 0.5, 0.5, 0, 8);

    var pdown = _global.ninePatchNode(down, 'h', 22, 22)
    pdown.setSize(width, 0);
    var cdown = new _Container();
    cdown.addChild(pdown);
    cdown.setSize(pic.getSize());
    var ref = new TextRef(text);
    ref.setPosition(0, -UPDOWN_OFFSET);
    cdown.addChild(ref);

    return _Button.createButtonWithId(id, cup, cdown);
};
exports.greenButton = function (id, text, scale) {
    return exports.createButton.apply(null, [R.ui.bt_green_up, R.ui.bt_green_down].concat(Array.prototype.slice.call(arguments, 0)));
};
exports.redButton = function (id, text, scale) {
    return exports.createButton.apply(null, [R.ui.bt_red_up, R.ui.bt_red_down].concat(Array.prototype.slice.call(arguments, 0)));
};
exports.createButton = function (up, down, id, text, scale) {
    var pic = _global.spriteNode(up);

    var text = S.ui.button.createText(text);
    text.setAnchor(0.5, 0.5);
    if (arguments.length > 4) {
        text.setScale(scale, scale);
    }

    var cup = new _Container();
    cup.addChild(pic);
    cup.setSize(pic.getSize());
    cup.addChild(text);
    _relative.local.layoutTo(text, 0.5, 0.5, cup, 0.5, 0.5, 0, 8);

    var cdown = new _Container();
    cdown.addChild(_global.spriteNode(down));
    cdown.setSize(pic.getSize());
    var ref = new TextRef(text);
    ref.setPosition(0, -UPDOWN_OFFSET);
    cdown.addChild(ref);

    return _Button.createButtonWithId(id, cup, cdown);
};
